const net = require('net');

const socket = net.createConnection({
  host: 'example.com',
  port: 80,
});

const { Buffer } = require('node:buffer');
// Need to keep around a few small chunks of memory.
const store = [];
socket.on('readable', () => {
  let data;
  while (null !== (data = readable.read())) {
    // Allocate for retained data.
    const sb = Buffer.allocUnsafeSlow(10);
    // Copy the data into the new allocation.
    data.copy(sb, 0, 0, 10);
    store.push(sb);
  }
});
